package com.fengshang.wedding.domain.service.internal;

import static com.fengshang.wedding.domain.Constants.GUEST_PASSWORD;
import static com.fengshang.wedding.domain.Constants.GUEST_SUFFIX;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.fengshang.wedding.domain.model.api.IUser;
import com.fengshang.wedding.domain.model.api.Role;
import com.fengshang.wedding.domain.model.internal.UserEntity;
import com.fengshang.wedding.domain.service.api.IUserManager;
import com.fengshang.wedding.domain.service.api.InvalidUserException;
import com.fengshang.wedding.domain.service.api.UserActionNotAllowedException;
import com.fengshang.wedding.domain.service.api.UserNotFoundException;
/**
 * 
 *@author gembin@gmail.com
 * 
 */
public class UserManager implements IUserManager {

	private IUserPersistenceService userPersistenceService;

	public void setUserPersistenceService(
			IUserPersistenceService userPersistenceService) {
		this.userPersistenceService = userPersistenceService;
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IUserManager#findUserById(int)
	 */
	public IUser findUserById(int userId)throws UserNotFoundException {
		if (userId < 0)
			return null;
		UserEntity userEntity = userPersistenceService.findUserById(userId);
		if (userEntity == null)
			 throw new UserNotFoundException("User with id="+userId+" dose not exists!");
		return  userEntity.getValueObject();
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IUserManager#findUserByName(java.lang.String)
	 */
	public IUser findUserByName(String name)throws UserNotFoundException {
		if (name == null)
			return null;
		UserEntity userEntity = userPersistenceService.findUserByName(name);
		if (userEntity == null)
			throw new UserNotFoundException("User with name="+name+" dose not exists!");
		return userEntity.getValueObject();
	}

	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IUserManager#findUsersByPagination(java.lang.String, int, int)
	 */
	public Collection<IUser> findUsersByPagination(String query, int pageSize,
			int currentPage) {
		Collection<UserEntity> users=userPersistenceService.findUserByPagination(query, pageSize, currentPage);
		Collection<IUser> usersVo=new ArrayList<IUser>();
		for(Iterator<UserEntity> it=users.iterator();it.hasNext();){
			usersVo.add(it.next().getValueObject());
		}
		return usersVo;
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IUserManager#getTotalPages(int)
	 */
	public int getTotalPages(int pageSize) {
		return userPersistenceService.getTotalPages(pageSize);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IUserManager#getTotalPages()
	 */
	public int getTotalPages() {
		return userPersistenceService.getTotalPages();
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IUserManager#removeUser(int)
	 */
	public void removeUser(int userId) throws UserNotFoundException,UserActionNotAllowedException{
		try{
			UserEntity userEntity = userPersistenceService.findUserById(userId);
			if(userEntity.getRole().equals(Role.ADMIN)){
				throw new UserActionNotAllowedException("ADMIN cannot be removed!!");
			}
			userPersistenceService.removeUser(userEntity);
		}catch (EntityNotFoundException e){
			throw new UserNotFoundException(e);
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IUserManager#createGuest(com.fengshang.wedding.domain.model.api.IUser)
	 */
	public void createGuest(IUser user)throws InvalidUserException{
		if(user==null)return;
		UserEntity userEntity=new UserEntity();
		if(user.getRole().equals(Role.ADMIN) || user.getRole().equals(Role.USER)){
			throw new InvalidUserException("User creation with role ADMIN or USER are not allowed!!");
		}
		userEntity.importData(user);
		//to avoid same guest name which is unique, because we don't check the name for guest
		userEntity.setName(new StringBuffer(user.getName())
		.append(GUEST_SUFFIX)
		.append(System.currentTimeMillis())//random string
		.toString());
		//password is required for database schema, we use a dummy password
		userEntity.setPassword(GUEST_PASSWORD);
		userEntity.setRole(Role.GUEST);
		userPersistenceService.createUser(userEntity);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IUserManager#createUser(com.fengshang.wedding.domain.model.api.IUser)
	 */
	public void createUser(IUser user)throws InvalidUserException {
		if(user==null)return;
		UserEntity userEntity=new UserEntity();
		if(user.getRole().equals(Role.ADMIN)){
			throw new InvalidUserException("User creation with role ADMIN is not allowed!!");
		}
		userEntity.importData(user);
		userEntity.setRole(Role.USER);
		userPersistenceService.createUser(userEntity);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IUserManager#updateUser(com.fengshang.wedding.domain.model.api.IUser)
	 */
	public IUser updateUser(IUser user)throws UserNotFoundException,InvalidUserException {
		if(user==null)return null;
		int userId=user.getId();
		UserEntity userEntity=userPersistenceService.findUserById(userId);
		if(userEntity==null)
			 throw new UserNotFoundException("User with id="+userId+" dose not exists!");
		userEntity.importData(user);
		if(userEntity.getRole().equals(Role.ADMIN)){
			throw new InvalidUserException("Update User role to ADMIN is not allowed!!");
		}
		return userPersistenceService.updateUser(userEntity).getValueObject();
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IUserManager#getTotalRecords()
	 */
	public int getTotalRecords() {
		return userPersistenceService.getTotalRecords();
	}
	

}
